Page({
    data: {
        num: 1,
        navtop: {},
        oldname: "", // 姓名
        oldaddress: "", // 身份证号
        oldphone: "", // 手机号           
        oldcity: "", // 详细地址
        show: false,
        region: [],
        openid: "",
        jhr: [],
        id: "",
        none:true,
    },
    gitfor() {
        wx.navigateBack({
            delta: -1
        })
    },
    //添加监护人
    getadd() {
      if(this.data.jhr.length<5){
        this.data.jhr.push({})
        this.setData({
            jhr: this.data.jhr
        })
      }
        
    },
    //删除监护人
    getsubtract() {
        this.data.jhr.pop()
        this.setData({
            jhr: this.data.jhr
        })
    },
    shanchu(e) {
        this.data.jhr.splice(e.currentTarget.dataset.id, 1)
        this.setData({
            jhr: this.data.jhr,
            id: e.currentTarget.dataset.jhrid
        })
        wx.request({
            url: 'https://dj.ifankui.com/api/usr/deleteJhr',
            method: "POST",
            data: {
                openid: this.data.openid,
                jhrId: this.data.id
            },
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success: (res) => {
                console.log(res);
            }
        })
    },
    //返回上一个页面
    gethui() {
        wx.navigateBack({
            delta: 1
        })
    },
    onLoad(options) {
        this.setData({
            openid: getApp().globalData.openid
        })
        // 导航栏
        // 1.获取屏幕可使用宽度
        let windowWidth = wx.getSystemInfoSync().windowWidth;
        // 2.获取状态栏高度
        const statusBarHeight = wx.getSystemInfoSync().statusBarHeight;
        //3.获取胶囊按钮位置信息
        const menuButton = wx.getMenuButtonBoundingClientRect();
        this.setData({
            navtop: menuButton.top
        })  
    },
    onShow() {
      const app=getApp()
        wx.request({
            url: 'https://dj.ifankui.com/api/usr/getProfile',
            method: "POST",
            data: {
                openid: this.data.openid
            },
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success: (res) => {
                // this.data.region.push(res.data.data.province,res.data.data.city,res.data.data.area)
              if(res.data.data.realname.length==0&&app.globalData.huancun.xingming.length!=0||app.globalData.huancun.jianhuren.length!=0){
                console.log(app.globalData.huancun.jianhuren);
                this.setData({
                  oldname:app.globalData.huancun.xingming,
                  oldphone: app.globalData.huancun.shoujihao,
                  oldaddress: app.globalData.huancun.shenfenzheng,
                  oldcity: app.globalData.huancun.xiangxidizhi,
                  jhr: app.globalData.huancun.jianhuren,
                  region:[app.globalData.huancun.shengshiqu[0],app.globalData.huancun.shengshiqu[1],app.globalData.huancun.shengshiqu[2]]
              })
              }else{
                this.setData({
                  oldname: res.data.data.lrxm,
                  oldphone: res.data.data.lrdh,
                  oldaddress: res.data.data.lrsfz,
                  oldcity: res.data.data.lrdz,
                  jhr: res.data.data.jhr,
                  region:[res.data.data.province,res.data.data.city,res.data.data.area]
              },()=>{                
                if(res.data.data.jhr.length==0){
                this.data.jhr.push({})
                 this.setData({
                     jhr:this.data.jhr
                 })
             }}) 
              }
              
                console.log(this.data.region);
            }
        })

    },
    bindRegionChange: function (e) {
      const app= getApp()
        console.log(e.detail.value)
        console.log(e.detail.code)
        this.setData({
            region: e.detail.value
        })
        app.globalData.huancun.shengshiqu=e.detail.value

    },
    xxx(e) {
      const that =this
        const item = e.detail.value
        const index = e.currentTarget.dataset
        console.log(item, index.index);
        this.setData({
            ['jhr[' + index.index + '].xm']: item
        },()=>{
          const app=getApp()
          app.globalData.huancun.jianhuren.xm=that.data.jhr});
      
        
    },
    xxx1(e) {
      const that =this
        const item = e.detail.value
        const index = e.currentTarget.dataset
        this.setData({
            ['jhr[' + index.index + '].dh']: item
        },()=>{
          const app=getApp()
          app.globalData.huancun.jianhuren=that.data.jhr
        });
        console.log(item, index.index);
     
    },
    xxx2(e) {
      const that =this
        const item = e.detail.value
        const index = e.currentTarget.dataset
        this.setData({
            ['jhr[' + index.index + '].dz']: item
        },()=>{ 
          const app=getApp()
          app.globalData.huancun.jianhuren=that.data.jhr});
        console.log(item, index.index);

    },
    // 处理老人姓名
    oldname(e) {
      
        console.log(e.detail.value);
        this.setData({
            oldname: e.detail.value
        })
        const app=getApp()
        app.globalData.huancun.xingming=e.detail.value
    },
    // 处理老人身份证号
    oldaddress(e) {
        this.setData({
            oldaddress: e.detail.value
        })
        const app=getApp()
        app.globalData.huancun.shenfenzheng=e.detail.value
    },
    // 处理老人手机号
    oldphone(e) {
        this.setData({
            oldphone: e.detail.value
        })
        const app=getApp()
        app.globalData.huancun.shoujihao=e.detail.value
    },
    // 处理老人详细地址
    oldcity(e) {
        this.setData({
            oldcity: e.detail.value
        })
        const app=getApp()
        app.globalData.huancun.xiangxidizhi=e.detail.value
    },
    formSubmit: function (e) {
        const that = this
        const {
            jhrname,
            jhrphone,
            oldname,
            oldaddress,
            oldphone,
            oldcity,
        } = e.detail.value;
        // 确保所有字段都不为空
        if (!oldname.trim()) {
            wx.showToast({
                title: '请填写老人姓名',
                icon: 'none'
            });
            return false; // 阻止表单提交
        }
        if (!oldaddress.trim()) {
            wx.showToast({
                title: '请填写老人身份证号',
                icon: 'none'
            });
            return false; // 阻止表单提交
        }
        if (!oldphone.trim()) {
            wx.showToast({
                title: '请填写老人电话',
                icon: 'none'
            });
            return false; // 阻止表单提交
        }
        if (!this.data.region[0]||!this.data.region[1]||!this.data.region[2]) {
            wx.showToast({
                title: '请填写省市区',
                icon: 'none'
            });
            return false; // 阻止表单提交
        }
        if (!oldcity.trim()) {
            wx.showToast({
                title: '请填写老人地址',
                icon: 'none'
            });
            return false; // 阻止表单提交
        }
        if(jhrname||jhrphone){
            if (!/^[\u4e00-\u9fa5]{2,10}$/.test(jhrname)) {
                wx.showToast({
                    title: '监护人姓名必须是2到10位汉字',
                    icon: 'none'
                });
                return false; // 阻止表单提交
            }
            if (!/^1[3-9]\d{9}$/.test(jhrphone)) {
                wx.showToast({
                    title: '监护人手机号格式不正确',
                    icon: 'none'
                });
                return false; // 阻止表单提交
            }
        }
        if (!/^[a-zA-Z\u4e00-\u9fa5]{2,10}$/.test(oldname)) {
            wx.showToast({
                title: '老人姓名必须是2到10位字符',
                icon: 'none'
            });
            return false; // 阻止表单提交
        }
        if (!/^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(oldaddress)) {
            wx.showToast({
                title: '身份证号格式不正确',
                icon: 'none'
            });
            return false; // 阻止表单提交
        }
        if (!/^1[3-9]\d{9}$/.test(oldphone)) {
            wx.showToast({
                title: '老人手机号格式不正确',
                icon: 'none'
            });
            return false; // 阻止表单提交
        }
        wx.request({
            url: 'https://dj.ifankui.com/api/usr/laorenBind',
            method: 'POST',
            data: {
                openid: this.data.openid,
                lrxm: this.data.oldname,
                lrsfz: this.data.oldaddress,
                lrdh: this.data.oldphone,
                province: this.data.region[0],
                city: this.data.region[1],
                area: this.data.region[2],
                lrdz: this.data.oldcity,
                jhr: JSON.stringify(this.data.jhr)
            },
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success: (res) => {
                console.log(res);
                if (res.data.status == 'ok') {
                    that.setData({
                        jhr: res.data.jhr
                    })
                    this.onShow()
                }
                wx.showToast({
                    title: res.data.msg,
                    icon: res.data.status == 'ok' ? 'success' : 'error'
                });
            }
        })
       
    }

})